package com.ruoyi.recycle.handler;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class TimeMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("开始填充默认值");
        final String username = SecurityUtils.getUsername();
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("createdTime", new Date(), metaObject);
        this.setFieldValByName("summitTime", new Date(), metaObject);
        this.setFieldValByName("createdBy", username, metaObject);
        final String identifyStatus = (String) this.getFieldValByName("identifyStatus", metaObject);
        if (StrUtil.isEmpty(identifyStatus)) {
            this.setFieldValByName("identifyStatus", "未认证", metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开始更新默认值");
        final String username = SecurityUtils.getUsername();
        this.setFieldValByName("updatedTime", new Date(), metaObject);
        this.setFieldValByName("updatedBy", username, metaObject);

    }
}
